package ext.drawing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.log4j.Logger;

import wt.util.WTProperties;

/**
 * 根据读取配置的excel判断是否为法定节假日，及法定工作日
 * 
 * @author lilin
 * 
 */
public class HolidayUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static List<String> holidays = new ArrayList<String>();
	private static List<String> workdays = new ArrayList<String>();
	private static String templateFolder = null;
	private static String tempFolderPath;
	private static Logger logger = Logger.getLogger(HolidayUtil.class);
	static {
		try {
			templateFolder = WTProperties.getLocalProperties().getProperty(
					"wt.home");
			tempFolderPath = templateFolder + File.separator + "codebase"
					+ File.separator + "ext" + File.separator + "drawing"
					+ File.separator + "data" + File.separator
					+ "holidaySet.xls";
			parseExcel(tempFolderPath);// 读取excel中的节假日和工作日
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 判断当天是否是工作日 (工作日：true；节假日：false)
	 * 
	 * @param currentDate
	 *            yyyy-MM-dd
	 * @return
	 * @throws ParseException
	 */
	public boolean isWorkDay(String currentDate) throws ParseException {
		boolean flag = true;
		int dateType = getDateType(currentDate);
		// 如果excel不存在当前日期。判断是否周六日
		if (dateType == 0) {

			Calendar c = Calendar.getInstance();
			c.setTime(sdf.parse(currentDate));
			if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
					|| c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				flag = false;
			}

		} else {// 如果存在当前日期，根据返回的类型判断

			if (dateType == 1) {// 节假日
				flag = false;
			}

			/*
			 * else if(dateType==2){//工作日 flag = true; }
			 */

		}
		logger.info("当前日期是：" + (flag == true ? "工作日" : "节假日"));

		return flag;
	}

	/**
	 * 根据判断当前时间是否是节假日还是工作日 (excel中不存在当前日期：0；节假日：1；工作日：2)
	 * 如果当前日期在excel中的节假日和工作日都写了，默认的工作日
	 * 
	 * @param currentDate
	 *            yyyy-MM-dd
	 * @return
	 */
	private int getDateType(String currentDate) {
		int type = 0;

		// String today = sdf.format(currentDate);

		if (holidays.size() > 0) {
			for (String holiday : holidays) {
				if (holiday.equals(currentDate)) {
					type = 1;
					break;
				}
			}
		}

		if (workdays.size() > 0) {
			for (String workday : workdays) {
				if (workday.equals(currentDate)) {
					type = 2;
				}
			}
		}

		return type;
	}

	/**
	 * 判断是否存在excel的日期（年） 
	 * true:存在
	 * false:不存在
	 * 
	 * @param currentDate
	 * 
	 * @return
	 * @throws ParseException
	 */
	public boolean isCurrentYear(String currentDate) throws ParseException {
//		System.out.println("HolidayUtil.............currentDate:" + currentDate);
		boolean flag = false;
		//法定节假日
		if (holidays.size() > 0) {
			for (String holiday : holidays) {
				if (holiday.contains(currentDate)) {
					flag = true;
					break;
				}
			}
		}
		//法定工作日
		if (workdays.size() > 0 && !flag) {
			for (String workday : workdays) {
				if (workday.contains(currentDate)) {
					flag = true;
					break;
				}
			}
		}
		
		return flag;
	}

	/**
	 * 读取excel中的节假日和工作日
	 * 
	 * @param filePath
	 */
	private static void parseExcel(String filePath) {
		if (filePath == null || "".equals(filePath)) {
			return;
		}
		Workbook workbook = null;
		try {
			workbook = Workbook.getWorkbook(new FileInputStream(filePath));

			Sheet sheets[] = workbook.getSheets();

			if (sheets == null || sheets.length == 0) {
				return;
			}
			Sheet sheet = sheets[0];// 取到第一个sheet
			int rows = sheet.getRows();
			// 获取第一列数据-节假日
			for (int rowIndex = 1; rowIndex < rows; rowIndex++) {
				Cell cell = sheet.getCell(0, rowIndex);
				if (cell == null) {
					break;
				}

				if (cell.getType() == CellType.DATE) {
					DateCell dateCell = (DateCell) cell;
					Date date = dateCell.getDate();
					String dateStr = sdf.format(date);
					holidays.add(dateStr);
				}

			}

			// 获取第二列数据-工作日
			for (int rowIndex = 1; rowIndex < rows; rowIndex++) {
				Cell cell = sheet.getCell(1, rowIndex);
				if (cell == null) {
					break;
				}
				if (cell.getType() == CellType.DATE) {
					DateCell dateCell = (DateCell) cell;
					Date date = dateCell.getDate();
					String dateStr = sdf.format(date);
					workdays.add(dateStr);
				}
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (workbook != null) {
				workbook.close();
				workbook = null;
			}
		}
	}

	public static void main(String[] args) throws ParseException {
		// TimeZone tz = WTContext.getContext().getTimeZone();
		// TimeZone.setDefault(tz);
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 保存格式
		String beginTimeStr = formatter.format(currentTime);

		// String end = BorrowUtil.increaseDateByDay(beginTimeStr, 3);

		// System.out.println(end);
		// HolidayUtil h = new HolidayUtil();
		// boolean flag = h.isWorkDay(new Date());
		// System.out.println(flag);
		// boolean flag = h.isWorkDay("");
	}
}